package com.cj.web.controller.ai_interview;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cj.ai.domain.*;
import com.cj.ai.service.IBannerService;
import com.cj.ai.service.ICategoryService;
import com.cj.ai.service.IWxLoginService;
import com.cj.ai.service.impl.AiService;
import com.cj.common.annotation.RateLimiter;
import com.cj.common.core.controller.BaseController;
import com.cj.common.core.domain.AjaxResult;
import com.cj.common.enums.LimitType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 和小程序相关的请求
 * @author cc
 * @date 2024-08-20 12:27
 **/
@RestController
@RequestMapping("/mp")
public class MpController extends BaseController {


    @Autowired
    IWxLoginService wxLoginService;

    @Autowired
    IBannerService bannerService;

    @Autowired
    ICategoryService categoryService;




    @PostMapping("/login")
    public AjaxResult hello(String code){
        WxLogin wxLogin = wxLoginService.login(code);
        if (wxLogin.getOpenid() != null){
            return success(wxLogin);
        }else{
            return AjaxResult.error(401,"尚未登录");
        }

    }


    @GetMapping("/banners")
    public List<Banner> getAllBanners(){
       return bannerService.list(new LambdaQueryWrapper<Banner>()
               .eq(Banner::getDelFlag,1).orderByAsc(Banner::getOrderNum)
               .select(Banner::getTitle,Banner::getImage,Banner::getUrl)
       );
    }


    @GetMapping("/categories")
    public List<Category> getAllCategory(){
        return categoryService.list(new LambdaQueryWrapper<Category>().eq(Category::getDelFlag,1).orderByAsc(Category::getOrderNum)
                // 获取指定的字段
                .select(Category::getText,Category::getSrc,Category::getName,Category::getEnable));
    }


    @Autowired
    AiService aiService;


    @PostMapping("/chat")
    @RateLimiter(key="mp-chat",time=3,count = 1,limitType = LimitType.IDENTITY)
    public MpAnswer chat(@RequestBody MpRequest mpRequest,@RequestHeader("openid") String openid){
        return aiService.chat(mpRequest,openid);
    }
}
